package cn.jiadakeji.parent.service.impl;

import cn.jiadakeji.parent.common.exception.ServiceException;
import cn.jiadakeji.parent.domain.*;
import cn.jiadakeji.parent.dto.SchoolClassDTO;
import cn.jiadakeji.parent.dto.SchoolDTO;
import cn.jiadakeji.parent.dto.StudentDTO;
import cn.jiadakeji.parent.mapper.StudentParentRelationMapper;
import cn.jiadakeji.parent.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author heyanfeng
 * @create 2022-03-09 16:02
 */
@Service
public class StudentParentRelationServiceImpl extends ServiceImpl<StudentParentRelationMapper, StudentParentRelation> implements StudentParentRelationService {

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentParentService parentService;

    @Autowired
    private SchoolClassService classService;

    @Autowired
    private SchoolService schoolService;

    /**
     * 查找当前家长关联的学生
     *
     * @return
     */
    @Override
    public List<StudentDTO> getStudentRelation(String parentId) {
        // 根据家长ID查询学生记录
        List<StudentParentRelation> relationList = baseMapper.selectList(new QueryWrapper<StudentParentRelation>()
                .eq("student_parent_id", parentId));
        // 查找学生信息
        return relationList.stream().map(item -> {
            Student student = studentService.getById(item.getStudentId());
            // 类型转换
            return conversion(student);
        }).collect(Collectors.toList());
    }

    /**
     * 获取SchoolDTO
     *
     * @param schoolId 学校ID
     */
    @NotNull
    public SchoolDTO getSchoolDTO(String schoolId) {
        School school = schoolService.getById(schoolId);
        SchoolDTO schoolDTO = new SchoolDTO();
        BeanUtils.copyProperties(school, schoolDTO);
        return schoolDTO;
    }

    /**
     * 获取SchoolClassDTO
     *
     * @param student 学生对象
     */
    @NotNull
    public SchoolClassDTO getSchoolClassDTO(Student student) {
        SchoolClass schoolClass = classService.getById(student.getSchoolClassId());
        SchoolClassDTO schoolClassDTO = new SchoolClassDTO();
        BeanUtils.copyProperties(schoolClass, schoolClassDTO);
        return schoolClassDTO;
    }

    /**
     * 类型转换
     * 将student对象转换为StudentDTO
     */
    @NotNull
    private StudentDTO conversion(Student student) {
        StudentDTO studentDTO = new StudentDTO();
        BeanUtils.copyProperties(student, studentDTO);
        // 设置出生日期
        studentDTO.setBirthDate(student.getBirthdate());
        // 设置班级信息
        SchoolClassDTO schoolClassDTO = getSchoolClassDTO(student);
        studentDTO.setSchoolClassDTO(schoolClassDTO);
        // 设置学校信息
        SchoolDTO schoolDTO = getSchoolDTO(schoolClassDTO.getSchoolId());
        studentDTO.setSchoolDTO(schoolDTO);
        return studentDTO;
    }


    /**
     * 确认关联学生
     *
     * @return
     */
    @Transactional
    @Override
    public Boolean confirmRelation(String parentId, String studentId) {
        // 首先查看当前学生有没有被关联过
        Integer count = baseMapper.selectCount(new QueryWrapper<StudentParentRelation>()
                .eq("student_id", studentId));
        if (count > 0) {
            throw new ServiceException("当前学生已有绑定信息，请您重新检查");
        }
        StudentParentRelation relation = new StudentParentRelation();
        relation.setStudentParentId(parentId);
        relation.setStudentId(studentId);
        // 查找家长信息
        StudentParent parent = parentService.getById(parentId);
        relation.setCreateBy(parent.getParentName());
        baseMapper.insert(relation);
        return true;

    }

    /**
     * 解除关联学生
     */
    @Transactional
    @Override
    public Boolean removeRelation(String parentId, String studentId) {
        // 查询当前关联的信息
        StudentParentRelation relation = baseMapper.selectOne(new QueryWrapper<StudentParentRelation>()
                .eq("student_parent_id", parentId)
                .eq("student_id", studentId));
        if (relation != null) {
            int byId = baseMapper.deleteById(relation);
            return byId != -1;
        }
        return false;
    }
}
